package edu.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import edu.domain.User;
import edu.service.UserService;
import edu.support.base.BasePage;
import edu.support.base.controller.AdminBaseController;
import edu.support.mybatis.MybatisWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * (User)表控制层
 *
 * @date  16:18:11
 */

@Controller
@RequestMapping("/admin/user")
public class UserAdminController extends AdminBaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 列表
     *
     * @param model
     * @param user
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, User user, BasePage rowPage) {
        QueryWrapper<User> wrapper = new MybatisWrapper<User>()
                .eq("role", "user")
                .like("username", user.getUsername())
                .like("Phone", user.getPhone())
                .like("Nickname", user.getNickname())
                .orderByDesc("create_time");
        PageInfo<User> pageInfo = userService.page(rowPage, wrapper);
        setModelAttribute(model, pageInfo);
        model.addAttribute(user);
        return "admin/user/list";
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public String del(@PathVariable Long id, RedirectAttributes attributes) {
        User user = userService.getById(id);
        userService.removeById(id);
        return redirect("/admin/user/list?role=" + user.getRole(), "删除成功", attributes);
    }


    /**
     * 详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail/{id}")
    public String detail(@PathVariable String id, Model model) {
        User user = userService.getById(id);
        if (null == user) {
            user = new User();
            user.setRole("student");
        }
        model.addAttribute("user", user);
        return "admin/user/detail";
    }

    /**
     * 更新
     *
     * @param user
     * @return
     */
    @RequestMapping("update")
    public String update(User user, RedirectAttributes attributes) {
        if (StringUtils.isNotBlank(user.getPassword())) {
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        } else {
            user.setPassword(null);
        }
        userService.saveOrUpdate(user);
        return redirect("/admin/user/list?role=" + user.getRole(), "操作成功", attributes);
    }

}